package com.zhbr.mvp.ui.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.ViewModelProvider;

import com.zhbr.mvp.base.view.BaseFragment;
import com.zhbr.ContextApplication;
import com.zhbr.mvp.base.presenter.BasePresenter;
import com.zhbr.mvp.ui.activity.AlterAccountActivity;
import com.zhbr.mvp.ui.activity.LoginActivity;
import com.zhbr.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyFragment extends BaseFragment {

    private MyViewModel myViewModel;

    @BindView(R.id.tableRow1)
    TableRow tableRow1;
    @BindView(R.id.tableRow2)
    TableRow tableRow2;
    @BindView(R.id.tableRow3)
    TableRow tableRow3;
    @BindView(R.id.tableRow4)
    TableRow tableRow4;

    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_phone)
    TextView tv_phone;

    public static MyFragment newInstance() {
        return new MyFragment();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void getViewModel() {
        myViewModel= new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MyViewModel.class);
    }


    @Override
    public Context getActivityContext() {
        return getContext();
    }

    @Override
    public int getContentView() {
        return R.layout.my_fragment;
    }

    @Override
    public void initView(View view) {
        //先绑定父view  上面的BindView才会生效
        ButterKnife.bind(this,view);
    }

    @Override
    public void initData() {
        tv_nickname.setText(myViewModel.getAccount().getActnickname());
        tv_phone.setText(myViewModel.getAccount().getActtelephone());
    }


    @OnClick({R.id.tableRow1,R.id.tableRow2,R.id.tableRow3,R.id.tableRow4,R.id.btn_logout})
    public void onViewClick(View view){
        Log.d("onViewClick", "点击事件："+view.getId());
        switch (view.getId()){
            case R.id.tableRow1:
                showToast("点击了【我的工单】");
                break;
            case R.id.tableRow2:
                showToast("点击了【我的班组】");
                break;
            case R.id.tableRow3:
                showToast("点击了【密码管理】");
                navigateTo(AlterAccountActivity.class);
                break;
            case R.id.tableRow4:
                showToast("点击了【设置】");
                break;
            case R.id.btn_logout:
                showLogoutDialog();
                break;
        }
    }

    public void showLogoutDialog(){
        AlertDialog.Builder builder= new AlertDialog.Builder(getActivityContext());
        builder.setTitle("是否退出登录？");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ContextApplication.getInstance().removeFromSP("user_info","token");
                navigateTo(LoginActivity.class);
            }
        });
        //设置按钮是否可以按返回键取消,false则不可以取消
        builder.setCancelable(true);
        AlertDialog dialog=builder.create();
        //设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐藏
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();

    }

}